package simulation;

import java.io.FileWriter;
import java.io.IOException;

public class FileLog {

	private static FileLog instance;
	private FileWriter log;
	private FileWriter simulationResults;
	private FileWriter simulationParameters;
	
	private FileLog(String simulationNumber){
		try {
			log = new FileWriter("log.txt", true); //true means to append file
			
//			if (simulationNumber.isEmpty()){
//				simulationResults = new FileWriter(System.getProperty("user.dir") + "/novasSimulacoes/resultadosX.txt", true); //true means to append file
//				simulationParameters = new FileWriter(System.getProperty("user.dir") + "/novasSimulacoes/parametrosX.txt", true); //true means to append file
//			}
//			else {
//				simulationResults = new FileWriter(System.getProperty("user.dir") + "/novasSimulacoes/resultados" + simulationNumber + ".txt", true); //true means to append file
//				simulationParameters = new FileWriter(System.getProperty("user.dir") + "/novasSimulacoes/parametros" + simulationNumber + ".txt", true); //true means to append file
//			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void appendStringLog(String str){
		try {
			if (log != null){
				log.write(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void appendStringResults(String str){
		try {
			if (simulationResults != null){
				simulationResults.write(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void appendStringParameters(String str){
		try {
			if (simulationParameters != null){
				simulationParameters.write(str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void closeFileLog(){
		try {
			if (log != null){
				log.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void closeFileResults(){
		try {
			if (simulationResults != null){
				simulationResults.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void closeFileParameters(){
		try {
			if (simulationParameters != null){
				simulationParameters.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void setInstance(FileLog instance) {
		FileLog.instance = instance;
	}

	public static FileLog openFile(String filename) {
		if (instance == null){
			instance = new FileLog(filename);
		}
		return instance;
	}
	
	public static void nullInstance(){
		instance = null;
	}
	
	public static FileLog getInstance() {
		if (instance == null){
			instance = new FileLog("");
		}
		return instance;
	}
	
	public void saveFinalStatsInFile(String filename, String data){
		FileWriter finalStats;
		try {
			finalStats = new FileWriter(System.getProperty("user.dir") + "/FinalStats/" + filename + ".txt", true);  //true means to append file
			
			if (finalStats != null){
				finalStats.write(data);
				finalStats.close();
			}
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	public void saveFinalStatsInFile(String path, String filename, String data){
		FileWriter finalStats;
		try {
			finalStats = new FileWriter(System.getProperty("user.dir") + "/" + path + "/" + filename + ".txt", true);  //true means to append file
		
			if (finalStats != null){
				finalStats.write(data);
				finalStats.close();
			}
					
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
}
